#pragma once
#include <io.h>
#include <vector>

using namespace std;

bool inline isWhiteSpace(char ch);

string& trim(string& str);

void Tokenize(const string& str,vector<string>& tokens,const string& delimiters);

class FileNameHelper
{
public:
	FileNameHelper()
	{

	}
	~FileNameHelper()
	{

	}

	int prepareNewName(string fullname)
	{
		if (fullname.length()<=1)
		{
			return -1;
		}
		src = fullname;
		cur = fullname;
		pathVector.clear();
		Tokenize(fullname,pathVector,"\\");
		mainName = pathVector.back();
		pathVector.pop_back();
		int pos = mainName.find_last_of('.');
		if (pos == -1)
		{
			extName = "";
		}
		else
		{
			extName = mainName.substr(pos);
			mainName = mainName.substr(0,pos);
		}
	}

	int prepareNewName(char * filename)
	{
		string str(filename);
		return prepareNewName(str);
	}

	void changeExtName(string ext)
	{
		old = true;
		extName = ext;
	}
	
	void changeMainName(string mname)
	{
		old = true;
		mainName = mname;
	}

	string getPath()
	{
		if (old)
			generatePath();
		old = false;
		return cur;
	}

	string getExtName(){return extName;}

	string getMainName(){return mainName;}

	static bool ifExist(const char* path){return (_access( path, 0 )) != -1; }

	static string getCurrentDirectory()
	{
		char buf[512];
		GetCurrentDirectory(512,buf);
		string str(buf);
		return str;
	}
protected:
	
private:
	string src;
	string cur;
	vector<string> pathVector;
	string mainName;
	string extName;

	bool old;

	void generatePath()
	{
		cur = "";
		vector<string>::iterator iter = pathVector.begin();
		for (;iter != pathVector.end(); iter++)
		{
			cur += *iter;
			cur += "\\";
		}
		cur += mainName;
		cur += extName;
	}
};